package androidx.test.espresso;

import android.view.View;
import androidx.core.os.EnvironmentCompat;
import androidx.test.espresso.core.internal.deps.guava.base.Preconditions;
import androidx.test.espresso.core.internal.deps.guava.collect.Lists;
import androidx.test.espresso.util.EspressoOptional;
import androidx.test.espresso.util.HumanReadables;
import java.util.List;
import org.hamcrest.Matcher;

/* loaded from: classes.dex */
public final class NoMatchingViewException extends RuntimeException implements EspressoException {
    private EspressoOptional<String> adapterViewWarning;
    private List<View> adapterViews;
    private boolean includeViewHierarchy;
    private View rootView;
    private Matcher<? super View> viewMatcher;

    private NoMatchingViewException(String description) {
        super(description);
        this.adapterViews = Lists.newArrayList();
        this.includeViewHierarchy = true;
        this.adapterViewWarning = EspressoOptional.absent();
    }

    private NoMatchingViewException(Builder builder) {
        super(getErrorMessage(builder), builder.cause);
        this.adapterViews = Lists.newArrayList();
        this.includeViewHierarchy = true;
        this.adapterViewWarning = EspressoOptional.absent();
        this.viewMatcher = builder.viewMatcher;
        this.rootView = builder.rootView;
        this.adapterViews = builder.adapterViews;
        this.adapterViewWarning = builder.adapterViewWarning;
        this.includeViewHierarchy = builder.includeViewHierarchy;
    }

    public String getViewMatcherDescription() {
        Matcher<? super View> matcher = this.viewMatcher;
        return matcher != null ? matcher.toString() : EnvironmentCompat.MEDIA_UNKNOWN;
    }

    private static String getErrorMessage(Builder builder) {
        if (builder.includeViewHierarchy) {
            String format = String.format("No views in hierarchy found matching: %s", builder.viewMatcher);
            if (builder.adapterViewWarning.isPresent()) {
                String valueOf = String.valueOf(format);
                String valueOf2 = String.valueOf((String) builder.adapterViewWarning.get());
                format = valueOf2.length() != 0 ? valueOf.concat(valueOf2) : new String(valueOf);
            }
            return HumanReadables.getViewHierarchyErrorMessage(builder.rootView, null, format, null);
        }
        return String.format("Could not find a view that matches %s", builder.viewMatcher);
    }

    /* loaded from: classes.dex */
    public static class Builder {
        private Throwable cause;
        private View rootView;
        private Matcher<? super View> viewMatcher;
        private List<View> adapterViews = Lists.newArrayList();
        private boolean includeViewHierarchy = true;
        private EspressoOptional<String> adapterViewWarning = EspressoOptional.absent();

        public Builder from(NoMatchingViewException exception) {
            this.viewMatcher = exception.viewMatcher;
            this.rootView = exception.rootView;
            this.adapterViews = exception.adapterViews;
            this.adapterViewWarning = exception.adapterViewWarning;
            this.includeViewHierarchy = exception.includeViewHierarchy;
            return this;
        }

        public Builder withViewMatcher(Matcher<? super View> viewMatcher) {
            this.viewMatcher = viewMatcher;
            return this;
        }

        public Builder withRootView(View rootView) {
            this.rootView = rootView;
            return this;
        }

        public Builder withAdapterViews(List<View> adapterViews) {
            this.adapterViews = adapterViews;
            return this;
        }

        public Builder includeViewHierarchy(boolean includeViewHierarchy) {
            this.includeViewHierarchy = includeViewHierarchy;
            return this;
        }

        public Builder withAdapterViewWarning(EspressoOptional<String> adapterViewWarning) {
            this.adapterViewWarning = adapterViewWarning;
            return this;
        }

        public Builder withCause(Throwable cause) {
            this.cause = cause;
            return this;
        }

        public NoMatchingViewException build() {
            Preconditions.checkNotNull(this.viewMatcher);
            Preconditions.checkNotNull(this.rootView);
            Preconditions.checkNotNull(this.adapterViews);
            Preconditions.checkNotNull(this.adapterViewWarning);
            return new NoMatchingViewException(this);
        }
    }
}
